package com.example.demo.service;

import com.example.demo.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

@Service
public class HttpUpload {
    private String newname;
    @Autowired
    private Random random;

    public String uppic( List<MultipartFile> upfile){
        if(upfile.size()>0&&!upfile.isEmpty()) {
            for(MultipartFile file:upfile) {
                String filename=file.getOriginalFilename();
                String dirpath="D:\\picture\\images\\java\\";
                File filepath=new File(dirpath);
                if(!filepath.exists()) {
                    filepath.mkdirs();
                }
                String code=random.randomCode();
                newname=code+filename;
                try {
                    file.transferTo(new File(dirpath+newname));
                } catch (Exception e) {
                    // TODO: handle exception
                    return "false";
                }
            }
            return "http://localhost:9090/images/java/"+newname;
        }else {
            return "false";
        }

    }
}
